<?php

namespace search\controllers\bigdata;

use business\elasticsearch\services\BigDataCustomerService;

class CustomerController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     *
     * @api {post} /bigdata/customer/get-list 找客户列表接口
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataCustomer
     *
     * @apiDescription 找客户
     *
     * @apiParam {Number} page                      页码
     * @apiParam {Number} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     */
    public function actionGetList()
    {

        try {
            $keyword = $this->getParam("keyword"); //关键词匹配公司名称、简称、曾用名、合作案例标题
            $companyNameKeyword = $this->getParam("company_name_keyword"); //关键词匹配公司名称
            $companyIdentity = $this->getParam("company_identity") ?: []; //企业属性
            $companyArea = $this->getParam("company_area") ?: [];//企业地区，省市混查
            $companyLevel = $this->getParam("company_level") ?: []; //企业等级
            $companyLabel = $this->getParam('company_label') ?: [];//企业标签
            $companyType = $this->getParam('company_type') ?: [];//企业类型
            $industryCategory = $this->getParam('industry_category') ?: [];//所在行业，三级
            $landUsageLabels = $this->getParam('land_usage_label') ?: [];//土地用途
            $fitmentFeeLabel = $this->getParam('fitment_fee_label') ?: [];//在售类型
            $projectTypeLabel = $this->getParam('project_type_label') ?: [];//工程用途
            $biddingArea = $this->getParam('bidding_area') ?: [];//中标地区，省市混查
            $creditLevel = $this->getParam('credit_level') ?: [];//信用评级
            $projectProvinceCode = $this->getParam("project_province_code");//项目所在省份code
            $projectCityCode = $this->getParam("project_city_code");//项目所在城市code
            $projectDateMin = $this->getParam("project_begin_date");
            $projectDateMax = $this->getParam("project_end_date");
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 100;

            $bigDataCustomerService = new BigDataCustomerService();
            $rs = $bigDataCustomerService->queryCustomerList(
                $keyword,
                $companyNameKeyword,
                $companyIdentity,
                $companyArea,
                $companyLevel,
                $companyLabel,
                $companyType,
                $industryCategory,
                $landUsageLabels,
                $fitmentFeeLabel,
                $projectTypeLabel,
                $biddingArea,
                $creditLevel,
                $projectProvinceCode, $projectCityCode, $projectDateMin, $projectDateMax,
                $page,
                $pageSize
            );
            return $this->success($rs);

        } catch (\Exception $ex) {
            \Yii::error("actionGetList，获取客户列表失败，错误信息：" . $ex);
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     *
     * @api {post} /bigdata/customer/detail 客户详情
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataCustomer
     *
     * @apiDescription 客户详情
     *
     * @apiParam {String} organizing_code               社会信用代码
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     */
    public function actionDetail()
    {
        try {
            $organizingCodes = $this->getParam('organizing_codes') ? explode(',', trim($this->getParam('organizing_codes'))) : [];
            if (empty($organizingCodes)) {
                return $this->error(-1, "参数错误");
            }

            $bigDataCustomerService = new BigDataCustomerService();
            $rs = $bigDataCustomerService->getCustomerDetail($organizingCodes);
            return $this->success($rs);

        } catch (\Exception $ex) {
            \Yii::error("actionDetail，获取客户详情失败，错误信息：" . $ex);
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    public function actionFuzzySearch()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $bigDataDeveloperService = new BigDataCustomerService();
            $rs = $bigDataDeveloperService->guessName($keyword, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/customer/get-customer-score 04、获取采购商的好客户得分
     *
     * @apiVersion 1.0.0
     * @apiGroup customer
     *
     * @apiDescription 获取采购商的好客户得分
     *
     * @apiParam {String} organizing_code               社会信用代码，多选，使用英文逗号拼接
     * @apiParam {String} subscription_land_usage       用户订阅的土地用途，多个用英文逗号拼接
     * @apiParam {String} subscription_project_type     用户订阅设置的项目在建类型，多个用英文逗号拼接
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     *
     *
     */
    public function actionGetCustomerScore()
    {
        try {
            $organizingCodes = $this->getParam('organizing_code') ? explode(',', trim($this->getParam('organizing_code'))) : [];
            $subscription = [];
            $subscription['land_usage_list'] = trim($this->getParam('subscription_land_usage')) ? explode(',', trim($this->getParam('subscription_land_usage'))) : [];
            $subscription['project_type_list'] = trim($this->getParam('subscription_project_type')) ? explode(',', trim($this->getParam('subscription_project_type'))) : [];

            $bigDataDeveloperService = new BigDataCustomerService();
            $rs = $bigDataDeveloperService->getDeveloperScore($organizingCodes, $subscription);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error("actionGetCustomerScore exception message:" . $ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }

    }
}
